package com.cacheserverdeploy.node;

import java.util.ArrayList;
import java.util.List;

/**
 * 网络节点
 * Created by Xingfeng on 2017-03-08.
 */
public class NetworkNode {

    private int id;
    private List<Link> links = new ArrayList<>();//表示出去的，从id出发到其他节点

    /**
     * 是否安置服务器，默认为false
     */
    private boolean isLocatedServer = false;

    /**
     * 是否是候选放置服务器的节点
     */
    private boolean isCandidate=false;

    /**
     * 相连的消费节点Id，为-1表示不相邻
     */
    private int comsumeNodeId = -1;
    
    
    
    /**使链路的可用流量加大
     * @param toID
     * @param discharge
     */
    public void plusDischarge(int toID,int discharge){
    	for (Link link : links) {
			if(link.getToId()==toID){
				link.setCurrentDischarge(link.getCurrentDischarge()-discharge);
				link.setExtraDischarge(link.getDischargeSum()-link.getCurrentDischarge());
				break;
			}
		}
    }
    
    /**获得链路价格
     * @param toID
     * @return
     */
    public int getLinkPrice(int toID) {
    	int price=0;
    	for (Link link : links) {
			if(link.getToId()==toID){
				price=link.getPrice();
				break;
			}
		}
    	return price;
	}
    
    /**这是消耗流量
     * @param toID
     * @param discharge
     */
    public void setDischargeTo(int toID,int discharge){
    	for (Link link : links) {
			if(link.getToId()==toID){
				link.setCurrentDischarge(link.getCurrentDischarge()+discharge);
				link.setExtraDischarge(link.getDischargeSum()-link.getCurrentDischarge());
				break;
			}
		}
    }
    
    /**让流量变为require
     * @param toID
     * @param discharge
     */
    public void DischargeGetSmall(int toID,int discharge) {
    	for (Link link : links) {
			if(link.getToId()==toID){
				link.setCurrentDischarge(discharge);
				link.setExtraDischarge(link.getDischargeSum()-link.getCurrentDischarge());
				break;
			}
		}
	}
    
    public void dischargeBefore(int toid){
    	for (Link link : links) {
			if(link.getToId()==toid){
				link.setCurrentDischarge(0);
				link.setExtraDischarge(link.getDischargeSum());
				break;
			}
		}
    }
    
    
    /**
     * 流量还原为初始状态
     */
    public void restoreDischarge() {
		for (Link link : links) {
			link.setCurrentDischarge(0);
			link.setExtraDischarge(link.getDischargeSum());
		}
	}
    
    
    /**到指定点最大
     * @param toID
     * @return
     */
    public int getMaxDischarge(int toID) {
    	int extraDischarge=0;
		for (Link link : links) {
			if (link.getToId()==toID) {
				extraDischarge=link.getDischargeSum();
			}
		}
		return extraDischarge;
	}
    
    /**剩余流量
     * @param toID
     * @return
     */
    public int getExtraDischarge(int toID) {
    	int extraDischarge=0;
		for (Link link : links) {
			if (link.getToId()==toID) {
				extraDischarge=link.getExtraDischarge();
				break;
			}
		}
		return extraDischarge;
	}
    
    

    public NetworkNode(int id) {
        this.id = id;
    }

    public void addLink(Link link) {
        links.add(link);
    }

    public void setComsumeNodeId(int comsumeNodeId) {
        this.comsumeNodeId = comsumeNodeId;
    }

	public List<Link> getLinks() {
		return links;
	}

	public void setLinks(List<Link> links) {
		this.links = links;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public boolean isLocatedServer() {
		return isLocatedServer;
	}

	public void setLocatedServer(boolean isLocatedServer) {
		this.isLocatedServer = isLocatedServer;
	}

	public boolean isCandidate() {
		return isCandidate;
	}

	public void setCandidate(boolean isCandidate) {
		this.isCandidate = isCandidate;
	}

	public int getComsumeNodeId() {
		return comsumeNodeId;
	}
}
